import java.util.Arrays;

public class PriorityQueue {
    public int[] elem;
    public int usedSize;

    public PriorityQueue() {
        this.elem=new int[10];
    }
    public void initElem(int[] array){
        for (int i = 0; i < array.length; i++) {
            elem[i]=array[i];
            usedSize++;
        }
    }

    /**
     *建堆的时间复杂度：
     *O(n)
     * @param array
     */
    public void createHeap(int[] array) {
        for (int parent = (usedSize-1-1)/2;  parent>=0 ;parent --) {
            shiftDown(parent,usedSize);
        }
    }
    public void createHeap2(int[] array){
        for (int i = 1; i < array.length; i++) {
            int child=i;
            int parent=(child-1)/2;
            while(child>0&&elem[child] >elem[parent]) {
                swap(child,parent);
                child=parent;
                parent=(child-1)/2;
            }
        }
    }

    /**
     *
     * @param root 是每棵子树的根节点的下标
     * @param len  是每棵子树调整结束的结束条件
     * 向下调整的时间复杂度：O(logn)
     */
    private void shiftDown(int root,int len) {
        int child=2*root+1;
        while(child<len){
            if(child+1<usedSize&&elem[child]<elem[child+1]){
                child++;
            }
            if(elem[root]<elem[child]) {
                swap(child, root);
                root = child;
                child = 2 * root+1;
            }else{
                break;
            }
        }

    }
    private void swap(int i,int j){
        int tmp=elem[i];
        elem[i]=elem[j];
        elem[j]=tmp;
    }


    /**
     * 入队：仍然要保持是大根堆
     * @param val
     */
    public void push(int val) {
        if(isFull()){
            elem= Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize]=val;
        usedSize++;
        int child=usedSize-1;
        shiftUp(child);
    }

    private void shiftUp(int child) {
        int parent=(child-1)/2;
        while(child>0){
            if(elem[child]>elem[parent]){
                swap(child,parent);
                child=parent;
                parent=(parent-1)/2;
            }else{
                break;
            }
        }
    }

    public boolean isFull() {
        return usedSize==elem.length;
    }
    /**
     * 出队【删除】：每次删除的都是优先级高的元素
     * 仍然要保持是大根堆
     */
    public void pollHeap() {
        if(isEmpty()){
            throw new EmptyException("堆为空,不可以删除");
        }
        swap(0,usedSize-1);
        usedSize--;
        shiftDown(0,usedSize);
    }

    public boolean isEmpty() {
        return usedSize==0;
    }

    /**
     * 获取堆顶元素
     * @return
     */
    public int peekHeap() {
        if(isEmpty()){
            throw new EmptyException("堆为空,不可以获取堆顶元素");
        }
        return elem[0];
    }
    public int[] fun(int[] array){
        createHeap(array);
        int end=usedSize-1;
        while(end!=0){
            swap(0,end);
            shiftDown(0,end-1);
            end=end-1;
        }
        int[] tmpp=new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmpp[i]=array[i];
        }
        return tmpp;
    }
}

